<?php

namespace App\Jobs\Injection;

use App\Models\Entity\Asset\Video\Vasseti;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Storage;
use DB;

class VassetiInjectionJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * 任务最大尝试次数
     *
     * @var int
     */
    public $tries = 3;

    /**
     * 任务运行的超时时间。
     *
     * @var int
     */
    public $timeout = 3600;

    /**
     * [$injectionWhere 注入的条件]
     * @var array
     */
    protected $injectionWhere = ['online_status' => 1, 'publish_status' => 0];

    /**
     * [$injectVassetiHistory description]
     * @var [type]
     */
    protected $injectVassetiHistory;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($injectVassetiHistory)
    {
        $this->injectVassetiHistory = $injectVassetiHistory;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // 开始时间
        $starttime = microtime(true);
        // 开始使用内存
        $startMemory = memory_get_usage();

        /**
         * 生成xml, 对上线的媒资进行注入
         */
        // 生成xml
        $uploadConfig = config('custom.base.upload.xml');
        $driver       = $uploadConfig['driver'];
        $dir          = $uploadConfig['dir'];
        $rootDir      = config('filesystems.disks.' . $driver . '.upload');

        // 完整路径
        // $fullPath = $rootDir . '/' . $dir . '/';
        $fullPath = $rootDir . '/';

        // 注入媒资总数
        $injectionVasseticount = 0;
        // 错误的行数
        $errorRow = [];

        /**
         * 配值
         */
        // ftp配置
        // $ftp_config = config('ftp.connections.connection1');
        $ftp_config         = config('ftp.connections.connection1');
        $ftp_config['host'] = '192.168.18.150:21';
        // 分块
        $injection_chunk_num = config('custom.base.job.vasseti.injection_chunk_num');
        // cp_id
        $config_media_injection_cp_id = config('custom.base.media_injection.cp_id');
        // url
        $config_media_injection_url = config('custom.base.media_injection.url');
        /**
         * xml注入, 500条一次处理
         */
        Vasseti::where($this->injectionWhere)->with(['vcat', 'actors', 'directors', 'vareas', 'vlangs', 'vyears', 'vcattags', 'vserialis'])->chunk($injection_chunk_num, function ($vassetis) use ($driver, $fullPath, $ftp_config, $config_media_injection_cp_id, $config_media_injection_url, &$injectionVasseticount, &$errorRow, &$i) {
            
            foreach ($vassetis as $vasseti) {
                // 完成数+1
                $this->injectVassetiHistory->increment('done_count');
                /**
                 * 数据准备
                 */
                $vasseti_id = $vasseti->id;
                // 媒资id
                $assetID = 'HDTY' . str_pad(udate('YmdHisu'),19 ,"0", STR_PAD_LEFT) . str_pad($vasseti_id, 9, "0", STR_PAD_LEFT);
                // 内容提供商id
                $providerID = 'HDTY';
                // 每次操作的序列号
                $serialNo = udate('YmdHisu');
                $name     = $vasseti->name;
                // $name = iconv("gbk", "utf-8", $name);
                // $name = iconv("GBK", "UTF-8//IGNORE", $name);
                // 视频文件名
                $vifile   = $vasseti->vifiles->first();
                if ($vifile){
                    $fileName = $vifile->name;
                    // 视频文件大小
                    $fileSize = $vifile->size;
                } else {
                    $fileName = '';
                    $fileSize = 0;
                }
                // 视频文件上传的ftp地址
                $upload_ftp_url = config('custom.base.media_injection.url');
                // 单位转换成byte
                $fileSize = $fileSize * 1024;
                // 四舍五入取整
                $fileSize = round($fileSize);
                $type     = 'VODRelease';
                $product  = 'VOD';
                /**
                 * xml生成
                 */
                // 创建version标签$version=$doc->createElement("version");$versionTextNode=$doc->createTextNode("1.0");$version->appendchild($versionTextNode);当<root>标签中有值时就可以显示完整的<root>标签了
                // header("Content-type: text/xml"); //头文件非常重要
                // 创建xml文件, 建立DOM对象
                $dom = new \DOMDocument('1.0', 'GBK');
                // 格式化输出，友好地保留缩进和空格
                $dom->formatOutput = true;

                // 建立<ADI>元素
                $adi = $dom->createElement('adi:ADI2');
                // 添加属性
                $adi->setAttribute('xmlns', 'http://www.cablelabs.com/VODSchema/default');
                $adi->setAttribute('xmlns:adi', 'http://www.cablelabs.com/VODSchema/adi');
                $adi->setAttribute('xmlns:vod', 'http://www.cablelabs.com/VODSchema/vod');
                // 把<ADI>元素追加到文档里面
                $dom->appendChild($adi);

                /**
                 * GroupAsset定义 start
                 */
                // 建立<GroupAsset>组对象
                $groupAsset = $dom->createElement('adi:OpenGroupAsset');
                $groupAsset->setAttribute('type', $type);
                $groupAsset->setAttribute('product', $product);
                $adi->appendChild($groupAsset);

                // 建立<VODRelease>元素并将其作为<GroupAsset>的子元素
                $vodRelease = $dom->createElement('vod:VODRelease');
                $vodRelease->setAttribute('providerID', $providerID);
                $vodRelease->setAttribute('providerType', '2');
                $vodRelease->setAttribute('assetID', $assetID);
                $vodRelease->setAttribute('updateNum', '');
                $vodRelease->setAttribute('groupAsset', 'Y');
                $vodRelease->setAttribute('serialNo', $serialNo);
                $groupAsset->appendChild($vodRelease);

                // 建立<AssetLifetime>元素并将其作为<VODRelease>的子元素
                $assetLifetime = $dom->createElement('adi:AssetLifetime');
                $assetLifetime->setAttribute('startDateTime', '2016-01-05');
                $assetLifetime->setAttribute('endDateTime', '2036-01-05');
                $vodRelease->appendChild($assetLifetime);

                /**
                 * GroupAssetd定义 end
                 */

                /**
                 * MetadataAsset定义 start
                 */
                // 建立<AddMetadataAsset>元数据对象
                $metadataAsset = $dom->createElement('adi:AddMetadataAsset');
                $metadataAsset->setAttribute('groupProviderID', $providerID);
                $metadataAsset->setAttribute('groupAssetID', $assetID);
                $metadataAsset->setAttribute('type', 'Title');
                $metadataAsset->setAttribute('product', $product);
                $adi->appendChild($metadataAsset);

                // 建立<Title>元素并将其作为<AddMetadataAsset>的子元素
                $title = $dom->createElement('vod:Title');
                $title->setAttribute('providerID', $providerID);
                $title->setAttribute('assetID', $assetID);
                $title->setAttribute('updateNum', '1');
                $metadataAsset->appendChild($title);

                // 建立<AssetLifetime>元素并将其作为<Title>的子元素
                $assetLifetime = $dom->createElement('adi:AssetLifetime');
                $assetLifetime->setAttribute('startDateTime', '2016-01-05');
                $assetLifetime->setAttribute('endDateTime', '2036-01-05');
                $title->appendChild($assetLifetime);

                // 建立<Keyword>元素并将其作为<Title>的子元素
                $keyword = $dom->createElement('vod:Keyword');
                $title->appendChild($keyword);
                // 建立<IsAdvertise>元素并将其作为<Title>的子元素
                $isAdvertise = $dom->createElement('vod:IsAdvertise');
                $title->appendChild($isAdvertise);
                $isAdvertiseValue = $dom->createTextNode(0);
                $isAdvertise->appendChild($isAdvertiseValue);
                // 建立<PlayTime>元素并将其作为<Title>的子元素
                $playTime = $dom->createElement('vod:PlayTime');
                $title->appendChild($playTime);
                // 建立<Emcee>元素并将其作为<Title>的子元素
                $emcee = $dom->createElement('vod:Emcee');
                $title->appendChild($emcee);
                // 建立<LastNameFirst>元素并将其作为<Emcee>的子元素
                $lastNameFirst = $dom->createElement('vod:LastNameFirst');
                $emcee->appendChild($lastNameFirst);
                // 建立<StudioName>元素并将其作为<Title>的子元素
                $studioName = $dom->createElement('vod:StudioName');
                $title->appendChild($studioName);
                // 建立<MetaLanguage>元素并将其作为<Title>的子元素
                $metaLanguage = $dom->createElement('vod:MetaLanguage');
                $title->appendChild($metaLanguage);
                // 建立<ExternalURL>元素并将其作为<Title>的子元素
                $externalURL = $dom->createElement('vod:ExternalURL');
                $title->appendChild($externalURL);
                // 建立<Actor>元素并将其作为<Title>的子元素
                $actor = $dom->createElement('vod:Actor');
                $actor->setAttribute('type', 'Actor');
                $title->appendChild($actor);
                // 建立<LastNameFirst>元素并将其作为<Emcee>的子元素
                $lastNameFirst = $dom->createElement('vod:LastNameFirst');
                $actor->appendChild($lastNameFirst);
                // 建立<RunTime>元素并将其作为<Title>的子元素
                $runTime = $dom->createElement('vod:RunTime');
                $title->appendChild($runTime);
                // 建立<SearchName>元素并将其作为<Title>的子元素
                $searchName = $dom->createElement('vod:SearchName');
                $title->appendChild($searchName);
                // 建立<EnglishName>元素并将其作为<Title>的子元素
                $englishName = $dom->createElement('vod:EnglishName');
                $title->appendChild($englishName);
                // 建立<ExternalNo>元素并将其作为<Title>的子元素
                $externalNo = $dom->createElement('vod:ExternalNo');
                $title->appendChild($externalNo);
                // 建立<Producer>元素并将其作为<Title>的子元素
                $producer = $dom->createElement('vod:Producer');
                $title->appendChild($producer);
                // 建立<LastNameFirst>元素并将其作为<Producer>的子元素
                $lastNameFirst = $dom->createElement('vod:LastNameFirst');
                $producer->appendChild($lastNameFirst);
                // 建立<Awards>元素并将其作为<Title>的子元素
                $awards = $dom->createElement('vod:Awards');
                $title->appendChild($awards);
                // 建立<MediaFormat>元素并将其作为<Title>的子元素
                $mediaFormat = $dom->createElement('vod:MediaFormat');
                $title->appendChild($mediaFormat);
                // 建立<Director>元素并将其作为<Title>的子元素
                $director = $dom->createElement('vod:Director');
                $title->appendChild($director);
                // 建立<LastNameFirst>元素并将其作为<Director>的子元素
                $lastNameFirst = $dom->createElement('vod:LastNameFirst');
                $director->appendChild($lastNameFirst);
                // 建立<CountryOfOrigin>元素并将其作为<Title>的子元素
                $countryOfOrigin = $dom->createElement('vod:CountryOfOrigin');
                $title->appendChild($countryOfOrigin);
                $countryOfOriginValue = $dom->createTextNode(0);
                $countryOfOrigin->appendChild($countryOfOriginValue);
                // 建立<Items>元素并将其作为<Title>的子元素
                $items = $dom->createElement('vod:Items');
                $title->appendChild($items);
                $itemsValue = $dom->createTextNode(0);
                $items->appendChild($itemsValue);
                // 建立<SourceInfo>元素并将其作为<Title>的子元素
                $sourceInfo = $dom->createElement('vod:SourceInfo');
                $title->appendChild($sourceInfo);
                // 建立<Comments>元素并将其作为<Title>的子元素
                $comments = $dom->createElement('vod:Comments');
                $title->appendChild($comments);
                // 建立<TitleFull>元素并将其作为<Title>的子元素
                $titleFull = $dom->createElement('vod:TitleFull');
                $title->appendChild($titleFull);
                $titleFullValue = $dom->createTextNode($name);
                $titleFull->appendChild($titleFullValue);
                // 建立<SourceType>元素并将其作为<Title>的子元素
                $sourceType = $dom->createElement('vod:SourceType');
                $title->appendChild($sourceType);
                // 建立<Agent>元素并将其作为<Title>的子元素
                $agent = $dom->createElement('vod:Agent');
                $title->appendChild($agent);
                // 建立<TitleBrief>元素并将其作为<Title>的子元素
                $titleBrief = $dom->createElement('vod:TitleBrief');
                $title->appendChild($titleBrief);
                // 建立<ShowType>元素并将其作为<Title>的子元素
                $showType = $dom->createElement('vod:ShowType');
                $title->appendChild($showType);
                $showTypeValue = $dom->createTextNode('Column');
                $showType->appendChild($showTypeValue);
                // 建立<Character>元素并将其作为<Title>的子元素
                $character = $dom->createElement('vod:Character');
                $title->appendChild($character);
                // 建立<Priority>元素并将其作为<Title>的子元素
                $priority = $dom->createElement('vod:Priority');
                $title->appendChild($priority);
                $priorityValue = $dom->createTextNode(10);
                $priority->appendChild($priorityValue);
                // 建立<Status>元素并将其作为<Title>的子元素
                $status = $dom->createElement('vod:Status');
                $title->appendChild($status);
                $statusValue = $dom->createTextNode(1);
                $status->appendChild($statusValue);
                // 建立<Guest>元素并将其作为<Title>的子元素
                $guest = $dom->createElement('vod:Guest');
                $title->appendChild($guest);
                // 建立<LastNameFirst>元素并将其作为<Guest>的子元素
                $lastNameFirst = $dom->createElement('vod:LastNameFirst');
                $guest->appendChild($lastNameFirst);
                // 建立<Theme>元素并将其作为<Title>的子元素
                $theme = $dom->createElement('vod:Theme');
                $title->appendChild($theme);
                // 建立<SummaryMedium>元素并将其作为<Title>的子元素
                $summaryMedium = $dom->createElement('vod:SummaryMedium');
                $title->appendChild($summaryMedium);
                $summaryMediumValue = $dom->createTextNode($name);
                $summaryMedium->appendChild($summaryMediumValue);
                // 建立<SummaryShort>元素并将其作为<Title>的子元素
                $summaryShort = $dom->createElement('vod:SummaryShort');
                $title->appendChild($summaryShort);
                $summaryShortValue = $dom->createTextNode('video');
                $summaryShort->appendChild($summaryShortValue);
                // 建立<Language>元素并将其作为<Title>的子元素
                $language = $dom->createElement('vod:Language');
                $title->appendChild($language);
                // 建立<Superviser>元素并将其作为<Title>的子元素
                $superviser = $dom->createElement('vod:Superviser');
                $title->appendChild($superviser);
                // 建立<EnglishDesc>元素并将其作为<Title>的子元素
                $englishDesc = $dom->createElement('vod:EnglishDesc');
                $title->appendChild($englishDesc);
                // 建立<Extend>元素并将其作为<Title>的子元素
                $extend = $dom->createElement('vod:Extend');
                $title->appendChild($extend);
                // 建立<JointMode>元素并将其作为<Extend>的子元素
                $jointMode = $dom->createElement('vod:JointMode');
                $extend->appendChild($jointMode);
                $jointModeValue = $dom->createTextNode(1);
                $jointMode->appendChild($jointModeValue);

                $metadataAsset = $dom->createElement('adi:AddMetadataAsset');
                $metadataAsset->setAttribute('groupProviderID', $providerID);
                $metadataAsset->setAttribute('groupAssetID', $assetID);
                $metadataAsset->setAttribute('type', 'CategoryPath');
                $metadataAsset->setAttribute('product', $product);
                $adi->appendChild($metadataAsset);
                // 建立<CategoryPath>元素并将其作为<Title>的子元素
                $categoryPath = $dom->createElement('vod:CategoryPath');
                $categoryPath->setAttribute('providerID', $providerID);
                $categoryPath->setAttribute('assetID', $assetID);
                $categoryPath->setAttribute('updateNum', '1');
                $metadataAsset->appendChild($categoryPath);
                // 建立<AssetLifetime>元素并将其作为<CategoryPath>的子元素
                $assetLifetime = $dom->createElement('adi:AssetLifetime');
                $assetLifetime->setAttribute('startDateTime', '2016-01-05');
                $assetLifetime->setAttribute('endDateTime', '2036-01-05');
                $categoryPath->appendChild($assetLifetime);
                // 建立<Category>元素并将其作为<CategoryPath>的子元素
                $category = $dom->createElement('vod:Category');
                $categoryPath->appendChild($category);
                $value = iconv("gbk", "utf-8", '体育栏目');
                // return $value;
                $categoryValue = $dom->createTextNode('体育栏目');
                $category->appendChild($categoryValue);
                // 建立<Classification>元素并将其作为<CategoryPath>的子元素
                $classification = $dom->createElement('vod:Classification');
                $categoryPath->appendChild($classification);
                $value               = iconv("gbk", "utf-8", '栏目');
                $classificationValue = $dom->createTextNode('栏目');
                $classification->appendChild($classificationValue);

                $metadataAsset = $dom->createElement('adi:AddMetadataAsset');
                $metadataAsset->setAttribute('groupProviderID', $providerID);
                $metadataAsset->setAttribute('groupAssetID', $assetID);
                $metadataAsset->setAttribute('type', 'Copyright');
                $metadataAsset->setAttribute('product', $product);
                $adi->appendChild($metadataAsset);
                // 建立<CategoryPath>元素并将其作为<Title>的子元素
                $copyright = $dom->createElement('vod:Copyright');
                $copyright->setAttribute('providerID', $providerID);
                $copyright->setAttribute('assetID', $assetID);
                $copyright->setAttribute('updateNum', '1');
                $metadataAsset->appendChild($copyright);
                // 建立<AssetLifetime>元素并将其作为<Copyright>的子元素
                $assetLifetime = $dom->createElement('adi:AssetLifetime');
                $assetLifetime->setAttribute('startDateTime', '2016-01-05');
                $assetLifetime->setAttribute('endDateTime', '2036-01-05');
                $copyright->appendChild($assetLifetime);
                // 建立<LicenseType>元素并将其作为<Copyright>的子元素
                $licenseType = $dom->createElement('vod:LicenseType');
                $copyright->appendChild($licenseType);
                $licenseTypeValue = $dom->createTextNode('0');
                $licenseType->appendChild($licenseTypeValue);
                // 建立<TransferAgain>元素并将其作为<Copyright>的子元素
                $transferAgain = $dom->createElement('vod:TransferAgain');
                $copyright->appendChild($transferAgain);
                // 建立<CopyType>元素并将其作为<Copyright>的子元素
                $copyType = $dom->createElement('vod:CopyType');
                $copyright->appendChild($copyType);
                $copyTypeValue = $dom->createTextNode('1');
                $copyType->appendChild($copyTypeValue);
                // 建立<OriginalLicenseCompany>元素并将其作为<Copyright>的子元素
                $originalLicenseCompany = $dom->createElement('vod:OriginalLicenseCompany');
                $copyright->appendChild($originalLicenseCompany);
                // 建立<Transfer3rd>元素并将其作为<Copyright>的子元素
                $transfer3rd = $dom->createElement('vod:Transfer3rd');
                $copyright->appendChild($transfer3rd);
                // 建立<CopyName>元素并将其作为<Copyright>的子元素
                $copyName = $dom->createElement('vod:CopyName');
                $copyright->appendChild($copyName);
                // 建立<PublishNo>元素并将其作为<Copyright>的子元素
                $publishNo = $dom->createElement('vod:PublishNo');
                $copyright->appendChild($publishNo);
                // 建立<AuthorserilNo>元素并将其作为<Copyright>的子元素
                $authorserilNo = $dom->createElement('vod:AuthorserilNo');
                $copyright->appendChild($authorserilNo);
                // 建立<VideoLicense>元素并将其作为<Copyright>的子元素
                $videoLicense = $dom->createElement('vod:VideoLicense');
                $copyright->appendChild($videoLicense);
                // 建立<CopyIdea>元素并将其作为<Copyright>的子元素
                $copyIdea = $dom->createElement('vod:CopyIdea');
                $copyright->appendChild($copyIdea);
                // 建立<CountryNo>元素并将其作为<Copyright>的子元素
                $countryNo = $dom->createElement('vod:CountryNo');
                $copyright->appendChild($countryNo);
                // 建立<PublicationDate>元素并将其作为<Copyright>的子元素
                $publicationDate = $dom->createElement('vod:PublicationDate');
                $copyright->appendChild($publicationDate);
                // 建立<CopyLicense>元素并将其作为<Copyright>的子元素
                $copyLicense = $dom->createElement('vod:CopyLicense');
                $copyright->appendChild($copyLicense);
                // 建立<CopySerilNo>元素并将其作为<Copyright>的子元素
                $copySerilNo = $dom->createElement('vod:CopySerilNo');
                $copyright->appendChild($copySerilNo);
                // 建立<CopyLicenser>元素并将其作为<Copyright>的子元素
                $copyLicenser = $dom->createElement('vod:CopyLicenser');
                $copyright->appendChild($copyLicenser);
                // 建立<TransferLicenseCompany>元素并将其作为<Copyright>的子元素
                $transferLicenseCompany = $dom->createElement('vod:TransferLicenseCompany');
                $copyright->appendChild($transferLicenseCompany);

                /**
                 * MetadataAsset定义 end
                 */
                /**
                 * ContentAsset定义 start
                 */
                $acceptContentAsset = $dom->createElement('adi:AcceptContentAsset');
                $acceptContentAsset->setAttribute('type', 'Video');
                $acceptContentAsset->setAttribute('metadataOnly', 'N');
                $acceptContentAsset->setAttribute('fileName', $fileName);
                $acceptContentAsset->setAttribute('fileSize', $fileSize);
                $acceptContentAsset->setAttribute('mD5CheckSum', '');
                $adi->appendChild($acceptContentAsset);
                // 建立<Video>元素并将其作为<AcceptContentAsset>的子元素
                $video = $dom->createElement('vod:Video');
                $video->setAttribute('providerID', $providerID);
                $video->setAttribute('assetID', $assetID . '-1');
                $video->setAttribute('updateNum', '1');
                $video->setAttribute('fileName', $fileName);
                $video->setAttribute('fileSize', $fileSize);
                $video->setAttribute('mD5CheckSum', '');
                $video->setAttribute('transferContentURL', $upload_ftp_url . '/' . $fileName);
                $video->setAttribute('encodingProfile', '');
                $video->setAttribute('encodingCode', '');
                $acceptContentAsset->appendChild($video);
                // 建立<AssetLifetime>元素并将其作为<Copyright>的子元素
                $assetLifetime = $dom->createElement('adi:AssetLifetime');
                $assetLifetime->setAttribute('startDateTime', '2016-01-05');
                $assetLifetime->setAttribute('endDateTime', '2036-01-05');
                $video->appendChild($assetLifetime);
                // 建立<AspectRatio>元素并将其作为<Copyright>的子元素
                $aspectRatio = $dom->createElement('vod:AspectRatio');
                $video->appendChild($aspectRatio);
                // 建立<HDFlag>元素并将其作为<video>的子元素
                $hdFlag = $dom->createElement('vod:HDFlag');
                $video->appendChild($hdFlag);
                $hdFlagValue = $dom->createTextNode(1);
                $hdFlag->appendChild($hdFlagValue);
                // 建立<Brightness>元素并将其作为<video>的子元素
                $brightness = $dom->createElement('vod:Brightness');
                $video->appendChild($brightness);
                // 建立<FrameHeight>元素并将其作为<video>的子元素
                $frameHeight = $dom->createElement('vod:FrameHeight');
                $video->appendChild($frameHeight);
                $frameHeightValue = $dom->createTextNode(576);
                $frameHeight->appendChild($frameHeightValue);
                // 建立<MimeType>元素并将其作为<video>的子元素
                $mimeType = $dom->createElement('vod:MimeType');
                $video->appendChild($mimeType);
                $mimeTypeValue = $dom->createTextNode(55);
                $mimeType->appendChild($mimeTypeValue);
                // 建立<IsEdit>元素并将其作为<video>的子元素
                $isEdit = $dom->createElement('vod:IsEdit');
                $video->appendChild($isEdit);
                $isEditValue = $dom->createTextNode(1);
                $isEdit->appendChild($isEditValue);
                // 建立<ServiceType>元素并将其作为<video>的子元素
                $serviceType = $dom->createElement('vod:ServiceType');
                $video->appendChild($serviceType);
                $serviceTypeValue = $dom->createTextNode(1);
                $serviceType->appendChild($serviceTypeValue);
                // 建立<NeedDRM>元素并将其作为<video>的子元素
                $needDRM = $dom->createElement('vod:NeedDRM');
                $video->appendChild($needDRM);
                $needDRMValue = $dom->createTextNode(1);
                $needDRM->appendChild($needDRMValue);
                // 建立<Usage>元素并将其作为<video>的子元素
                $usage = $dom->createElement('vod:Usage');
                $video->appendChild($usage);
                $usageValue = $dom->createTextNode(1);
                $usage->appendChild($usageValue);
                // 建立<FrameWidth>元素并将其作为<video>的子元素
                $frameWidth = $dom->createElement('vod:FrameWidth');
                $video->appendChild($frameWidth);
                $frameWidthValue = $dom->createTextNode(720);
                $frameWidth->appendChild($frameWidthValue);
                // 建立<VideoCodec>元素并将其作为<video>的子元素
                $videoCodec = $dom->createElement('vod:VideoCodec');
                $video->appendChild($videoCodec);
                // 建立<Numberofframes>元素并将其作为<video>的子元素
                $numberofframes = $dom->createElement('vod:Numberofframes');
                $video->appendChild($numberofframes);
                $numberofframesValue = $dom->createTextNode(0);
                $numberofframes->appendChild($numberofframesValue);
                // 建立<AudioCodec>元素并将其作为<video>的子元素
                $audioCodec = $dom->createElement('vod:AudioCodec');
                $video->appendChild($audioCodec);
                // 建立<IsDRM>元素并将其作为<video>的子元素
                $isDRM = $dom->createElement('vod:IsDRM');
                $video->appendChild($isDRM);
                $isDRMValue = $dom->createTextNode(1);
                $isDRM->appendChild($isDRMValue);
                // 建立<Duration>元素并将其作为<video>的子元素
                $duration = $dom->createElement('vod:Duration');
                $video->appendChild($duration);
                $durationValue = $dom->createTextNode(180);
                $duration->appendChild($durationValue);
                // 建立<FileType>元素并将其作为<video>的子元素
                $fileType = $dom->createElement('vod:FileType');
                $video->appendChild($fileType);
                $fileTypeValue = $dom->createTextNode(1);
                $fileType->appendChild($fileTypeValue);
                // 建立<Contrast>元素并将其作为<video>的子元素
                $contrast = $dom->createElement('vod:Contrast');
                $video->appendChild($contrast);
                // 建立<Bitrate>元素并将其作为<video>的子元素
                $bitrate = $dom->createElement('vod:Bitrate');
                $video->appendChild($bitrate);
                $bitrateValue = $dom->createTextNode(1600);
                $bitrate->appendChild($bitrateValue);
                // 建立<FrameRate>元素并将其作为<video>的子元素
                $frameRate = $dom->createElement('vod:FrameRate');
                $video->appendChild($frameRate);
                $frameRateValue = $dom->createTextNode(25);
                $frameRate->appendChild($frameRateValue);
                /**
                 * ContentAsset定义 end
                 */
                /**
                 * AssociateContent定义 start
                 */
                $associateContent = $dom->createElement('adi:AssociateContent');
                $associateContent->setAttribute('type', 'Video');
                $associateContent->setAttribute('effectiveDate', '');
                $associateContent->setAttribute('groupProviderID', $providerID);
                $associateContent->setAttribute('groupAssetID', $assetID);
                $associateContent->setAttribute('providerID', $providerID);
                $associateContent->setAttribute('assetID', $assetID . '-1');
                $adi->appendChild($associateContent);
                /**
                 * AssociateContent定义 end
                 */
                // 保存到xml
                // 完整路径
                $fullPath1 = $fullPath . 'vassetis' . $vasseti_id . '.xml';
                $xml = $dom->saveXML();
                // $xml = iconv('utf-8', 'gbk', $dom->saveXML());
                $bool      = Storage::disk($driver)->put($fullPath1, $xml);
                // $bool      = Storage::disk('ftp')->put($fullPath1, $xml);
                if ($bool) {
                    Vasseti::where('id', '=', $vasseti_id)->update(['msg_id' => $assetID, 'sync_result' => 1, 'async_result' => 0]);
                    // 注入媒资总数
                    $injectionVasseticount++;
                } else {
                    // 错误的媒资id
                    $errorRow[] = $vasseti_id;
                }
            }
        });
        // 结束时间
        $endtime        = microtime(true);
        $consuming_time = round($endtime - $starttime, 4);
        $endMemory      = memory_get_usage();
        $usedMemory     = ($endMemory - $startMemory) / 1024;

        $flag       = true;
        $errorCount = count($errorRow);
        $message    = '<div style="word-wrap: break-word;">';
        $message .= '耗时:';
        $message .= $consuming_time . '秒<br />';
        $message .= '耗费内存:';
        $message .= "{$usedMemory} K<br />";
        $message .= '注入总数:';
        $message .= $this->injectVassetiHistory->count . '<br />';
        $message .= '注入成功数:';
        $message .= $injectionVasseticount . '<br />';
        $message .= '注入失败数:';
        $message .= count($errorRow) . '<br />';
        if ($errorCount > 0) {
            $flag = false;
            $message .= '注入失败的媒资id:' . implode(',', $errorRow) . '<br />';
        }
        $message .= '</div>';
        //通过标志位判断前端返回时成功还是失败
        if ($flag) {
            $message = '<div class="alert alert-success">' . $message . '</div>';
        } else {
            $message = '<div class="alert alert-error">' . $message . '</div>';
        }
        $this->injectVassetiHistory->consuming_time = $consuming_time;
        $this->injectVassetiHistory->used_memory    = $usedMemory;
        $this->injectVassetiHistory->content        = $message;
        $this->injectVassetiHistory->save();
    }
}
